﻿using System;
using System.Linq;
using System.Xml.Serialization;

namespace CXW.Core.Util
{
    [Serializable]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class ViewTemplate
    {
        [XmlAttribute()]
        public string Name { set; get; }

        /// <remarks/>
        [XmlArrayItem("Column", IsNullable = false)]
        public ViewColumn[] Columns;
        public ViewColumn this[string dataField]
        {
            get { return Columns.FirstOrDefault(item => item.DataField.Equals(dataField, StringComparison.OrdinalIgnoreCase)); }
        }
        [XmlAttribute()]
        public bool Editable { set; get; }

        private bool _DefaultContextMenu = true;
        /// <summary>
        /// 是否使用显示默认的右键菜单(默认显示)
        /// </summary>
        [XmlAttribute()]
        public bool DefaultContextMenu
        {
            get { return _DefaultContextMenu; }
            set { _DefaultContextMenu = value; }
        }

        private bool showAutoFilterRow = true;
        /// <summary>
        /// 是否显示过滤行（dev第一行）
        /// </summary>
        [XmlAttribute()]
        public bool ShowAutoFilterRow
        {
            set { showAutoFilterRow = value; }
            get { return showAutoFilterRow; }
        }

        [XmlAttribute()]
        public bool MultiSelect;

        [XmlAttribute()]
        public string Caption;
    }

    public class ViewColumns
    {
        [XmlArrayItem("Column", IsNullable = false)]
        public ViewColumn[] Columns;
    }

    public enum CtrlType
    {
        None,
        TextBox,
        DateTime,
        Form,
        CheckBox,
        ComboBox,
        DropdownList
    }

    public class ViewColumn
    {
        [XmlAttribute()]
        public string DataField;

        [XmlAttribute()]
        public string TargetKey;

        [XmlAttribute()]
        public string Caption;

        [XmlAttribute()]
        public CtrlType CtrlType;

        [XmlAttribute()]
        public string DicCodeType;

        [XmlAttribute()]
        public string DisplayMember = "CodeName";

        [XmlAttribute()]
        public string ValueMember = "CodeId";

        [XmlAttribute("AllowEdit")]
        public string Editable { set; get; }

        /// <summary>
        /// XmlSerializer 反序列化成对象后，
        /// 根据Editable值，和父级对象的Editale值，二次处理AllowEdit
        /// （一般涉及上下级赋值问题，不直接用XmlSerializer）
        /// </summary>
        public bool AllowEdit { set; get; }

        /// <summary>
        /// 四则运算公式
        /// <remarks>
        /// 多个表达式用";"分开，后期通过语法解析树，执行运算。
        /// </remarks>
        /// <example>
        /// 用实体属性或列字段描述 shl=bzshl*jlgg+lsshl
        /// 表示：【数量】=【包装数量】*【计量规格】+【零散数量】
        /// </example>
        /// </summary>
        [XmlAttribute()]
        public string Formula { set; get; }

        private bool visible = true;

        [XmlAttribute()]
        public bool Visible
        {
            set { visible = value; }
            get { return visible; }
        }

        [XmlAttribute()]
        public string DataType { set; get; }

        [XmlAttribute()]
        public int VisibleIndex { set; get; }

        [XmlAttribute()]
        public int RowIndex { get; set; }

        [XmlAttribute()]
        public int DataLength { set; get; }

        private int width = 120;

        [XmlAttribute()]
        public int Width
        {
            set { width = value; }
            get { return width; }
        }

        [XmlAttribute()]
        public int DataScale { set; get; }

        [XmlAttribute()]
        public string Measure { set; get; }

        [XmlAttribute()]
        public bool IsSort { set; get; }

        [XmlAttribute()]
        public bool IsFilter { set; get; }
        /// <summary>
        /// 编辑状态时，Enter操作跳转到的列字段DataField值
        /// </summary>
        [XmlAttribute()]
        public string Navigate { set; get; }

        [XmlAttribute()]
        public bool IsLock { set; get; }

        [XmlAttribute()]
        public bool NotNull { set; get; }

        [XmlAttribute()]
        public string Format { get; set; }

        private bool autoWidth = true;

        [XmlAttribute()]
        public bool AutoWidth
        {
            set { autoWidth = value; }
            get { return autoWidth; }
        }
        /// <summary>
        /// 是否数据库验证
        /// </summary>
        [XmlAttribute()]
        public bool IsVerify { set; get; }
        /// <summary>
        /// 正则表达式验证
        /// </summary>
        [XmlAttribute()]
        public string VerifyReg { set; get; }

        [XmlAttribute()]
        public int MaxLength { get; set; }
        /// <summary>
        /// 是否合并弹窗选择的多行数据
        /// 以Separator拼接
        /// </summary>
        [XmlAttribute()]
        public bool IsMerge { set; get; }

        /// <summary>
        /// 当IsMerge=true时，以此值作为分隔符
        /// </summary>
        [XmlAttribute()]
        public string Separator = ";";
    }
}
